<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//测试控制器
class ContentController extends Controller{
	//构造函数
	public function __init(){
	}
	
    //动作
    public function index(){
        $gid = Q('get.gid',0,'intval');
        $sid = Q('get.cid',0,'intval');
            //顶部面包屑指向
        $parent = Data::parentChannel(Db::table("category")->get(), $sid, $fieldPri = 'cid', $fieldPid = 'pid');
        $parents = array();
        $parents = array_reverse($parent);
        View::with('parent',$parents);
            //获取当前商品的规格------------------------------------------
        //获取taid和taname;
        $select_menu = Db::select("select taname,group_concat(gaid) as item_id,group_concat(gavalue) as item from goods_attr join type_attribute on type_attribute_taid=taid where goods_gid=$gid and class=2 group by taid ");
        $gaid = array();
        foreach ($select_menu as $k => &$xxv) {
            $xxv['item'] = explode(',', $xxv['item']);
            $gaid[] = explode(',', $xxv['item_id']);

        }
        foreach($select_menu as $k=>$vk){
            foreach($vk['item'] as $kk=>$vit){
                $select_menu[$k]['item_son'][$gaid[$k][$kk]] = $vit;
            }
        }
        View::with('select_menu',$select_menu);
           
            //获取商品详情表里面的图册地址--------------------------------------
        $album = Db::table("goods_detail")->where("goods_gid = {$gid}")->pluck("album");
            //转成数组
        $album = explode(',', $album);
            //截取数组中的每个地址，换成缩略后的图片地址
            //小图
        $small = array();
        foreach($album as $k => $value){
            $str = substr($value,15,6);
            $small[] =$str.'s_'.substr($value,21);
        }
            //中图
        $middle=array();
        foreach($album as $K => $v){
            $str = substr($v,15,6);
            $middle[] =$str.'m_'.substr($v,21);
        }
            //大图
        $big = array();
        foreach($album as $K => $va){
            $str = substr($va,15,6);
            $big[] =$str.'b_'.substr($va,21);
        }
            //分配图片
        View::with('small',$small);
        View::with('middle',$middle);
        View::with('big',$big);
            //获取当前的商品的信息-------------------------------------------
        $data = Db::table("goods")->join("goods_detail",'gid','=','goods_gid')->where("gid={$gid}")->get();
            //获取左下火热商品的所有信息----------------------------------------
        $dataAll = Db::table("goods")->orderBy("click",'DESC')->limit(5)->get();
        View::with('dataAllaa',$dataAll);
        View::with('data',$data[0]);
        View::make();
    }
        //ajax提交查询
    public function getAjax(){
        if(IS_AJAX){
                //获取规格ID
            $gaid = Q('post.gaid');
              //获取商品ID
            $gid = Q('post.gid');
                //转换 |  为逗号
            $gaidv = str_replace('|', ',', $gaid);
            $gaModel = new \Admin\Model\Goods_attr;
            $gaData = array();
                //获取所选货品的规格
            $gaData = $gaModel->where("goods_gid={$gid} and gaid in ($gaidv)")->get();
                //获取当前的价格
            $price = Db::table("goods")->where("gid={$gid}")->pluck("shop_price");
                //实例化货品表
            $listModel = new \Admin\Model\Goods_list;
                //找到所选的货品是否有货
            $data = $listModel->where("goods_gid={$gid} AND combine='{$gaid}'")->find();
                //如果无货，则返回无货
            if(!$data){
                echo json_encode(array('status'=>true,'message'=>"该型号商品无货"));
            }else{//否则有货
                    //把得到的组合ID拆分开
                //把基本价格压入data
                $data['price']=$price;
                $gaid = explode('|', $data['combine']);
                    //获得每个规格的加价
                $gavalue = array();
                foreach($gaid as $kg => $vd){
                    $data['price'] +=$gaData[$kg]['value_added'];
                    $gavalue[]=$gaData[$kg]['gavalue'];
                }
                $gavalue = implode('/',$gavalue);
                $data['combine']=$gavalue;
                echo json_encode($data);
            }
                exit;
        }
    }
	
}
